const md5 = require('md5')
const executeSql = require('./utils/query.js')
const { PASSWORD_SALT } = require('./config/index.js')

exports.getUserInfo = async (user_id) => {
  // 1. 查数据库
  let sql = `select id as userId,username,avatar,nickname from users where id = ${user_id}`
  let result = await executeSql(sql)
  return result
}

exports.Login = async (username, password) => {
  password = md5(password + PASSWORD_SALT)
  // 操作数据库 ,编写sql,真实业务中，肯定要去数据库中匹配
  let sql = `select * from users where username='${username}' and password = '${password}' limit 1`
  let result = await executeSql(sql)
  return result
}

exports.getOrderData = async (user_id) => {
  let sql = `select * from goods_order where user_id = ${user_id}`
  let result = await executeSql(sql)
  return result
}

exports.getUserInfoById = async (id) => {
  // 操作数据库 ,编写sql,真实业务中，肯定要去数据库中匹配
  let sql = `select * from users where id = ${id} limit 1`
  let result = await executeSql(sql)
  return result
}

exports.getAddressData = () => {
  // 操作数据库 ,编写sql
  return [
    { id: 1, text: '1号地址' },
    { id: 2, text: '1号地址' },
    { id: 3, text: '1号地址' },
  ]
}
